package com.lepu.app.fun.index.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.UmengEvent;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.app.fun.index.fragment.FragmentFileLog;
import com.lepu.app.fun.index.fragment.FragmentFileReport;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class IndexFileActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener {
	
	private ViewPager mViewPager = null;
	private ArrayList<LinearLayout> mTabLayoutList;
	public ArrayList<TextView> mTextList;
	
	public ArrayList<Fragment> fragmentList;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedDefaultUmeng = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_file_activity);

		init();
	}
	
    @Override
	public void onResume() {
        super.onResume();

        UmengHelper.onResume(this, UmengEvent.page_shouye_jiankangdangan);
    }

    @Override
	public void onPause() {
        super.onPause();

        UmengHelper.onPause(this, UmengEvent.page_shouye_jiankangdangan);
    }

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}
	
	@Override
	public void onTopbarRightButtonSelected() {
		if(mViewPager.getCurrentItem() == 0){

			Intent intent = new Intent(this, IndexFileLogAddActivity.class);
			startActivity(intent,true);
			UmengHelper.CustomEvent(this, UmengEvent.event_wode_jianchabaogao_tianjia_jiankangrizhi);
		}else if(mViewPager.getCurrentItem() == 1){

			Intent intent = new Intent(this, IndexFileReportAddActivity.class);
			startActivity(intent,true);
			UmengHelper.CustomEvent(this, UmengEvent.event_wode_jianchabaogao_tianjia_jianchabaogao);
		}
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("健康档案");
		topbar.setRightText("添加");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		mTabLayoutList = new ArrayList<LinearLayout>();
		mTextList = new ArrayList<TextView>();
		
		LinearLayout layout1 = (LinearLayout) findViewById(R.id.tabLayout1);
		LinearLayout layout2 = (LinearLayout) findViewById(R.id.tabLayout2);
		
		mTabLayoutList.add(layout1);
		mTabLayoutList.add(layout2);
		
		TextView text1 = (TextView) findViewById(R.id.tabText1);
		TextView text2 = (TextView) findViewById(R.id.tabText2);
		
		mTextList.add(text1);
		mTextList.add(text2);
		
		for(int i=0; i<mTabLayoutList.size(); i++){
			mTabLayoutList.get(i).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					for(int k=0; k<mTabLayoutList.size(); k++){
						if(mTabLayoutList.get(k).getId() == v.getId()){
							mTabLayoutList.get(k).setSelected(true);
							mViewPager.setCurrentItem(k,false);
							//mTextList.get(k).setTextColor(getResources().getColor(R.color.color_white));
						}else{
							mTabLayoutList.get(k).setSelected(false);
							//mTextList.get(k).setTextColor(getResources().getColor(R.color.color_black));
						}
					}
				}
			});
		}
		
		fragmentList = new ArrayList<Fragment>();
		FragmentFileLog bbs1 = new FragmentFileLog();
		FragmentFileReport bbs2 = new FragmentFileReport();
		
		fragmentList.add(bbs1);
		fragmentList.add(bbs2);
		
		mViewPager = (ViewPager)findViewById(R.id.motherCircleListViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
		mViewPager.setOffscreenPageLimit(5);
		
		int index = getIntent().getIntExtra("index", 0);
		
		mViewPager.setCurrentItem(index,false);
		mTabLayoutList.get(index).setSelected(true);
		//mTextList.get(index).setSelected(true);
	}

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mViewPager.setCurrentItem(index,false);

			if(index == 0){
				UmengHelper.CustomEvent(IndexFileActivity.this, UmengEvent.event_wode_jianchabaogao_jiankangrizhi);
			}else if(index == 1){
				UmengHelper.CustomEvent(IndexFileActivity.this, UmengEvent.event_wode_jianchabaogao_jianchabaogao);
			}
			
			for(int i=0; i<mTabLayoutList.size(); i++){
				if(i == index){
					mTabLayoutList.get(i).setSelected(true);
					//mTextList.get(i).setTextColor(getResources().getColor(R.color.color_white));
				}else{
					mTabLayoutList.get(i).setSelected(false);
					//mTextList.get(i).setTextColor(getResources().getColor(R.color.color_black));
				}
			}
		}
	}

}
